package com.medical.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.medical.HttpTools.DataAnalyze;
import com.medical.HttpTools.HttpRequest;
import com.medical.po.Hospitalpackage;
import com.medical.po.HospitalpackageCustom;
import com.medical.po.Hospitalunitprice;
import com.medical.service.PackageService;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年8月21日 下午4:00:59 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/packageMan")
public class PackageController {
	@Autowired
	private PackageService packageService;
	/**
	 * 初始化套餐设置界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/packageSet")
	public ModelAndView packageSet()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/PackageMan/PackageSet");
		return model;
	}
	/**
	 * 插入表记录，套餐设置提交类，插入到表hospitalpackage
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/packageSetSubmit")
	public @ResponseBody Map<String,Object> packageSetSubmit(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		
		String packagename = request.getParameter("packagename");
		String packagenumber = request.getParameter("packagenumber");
		String packageabs = request.getParameter("packageabs");
		String packagesustime = request.getParameter("packagesustime");
		String packageoperatetime = request.getParameter("packageoperatetime");
		String param = "packagename="+packagename+"&packagenumber="+packagenumber+"&packageabs="+packageabs+"&packagesustime="+packagesustime+"&packageoperatetime="+packageoperatetime+"&hosploginid="+hosploginid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("packageset", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg",jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误，套餐设置失败！");
		}
		return result;
	}
	/**
	 * 套餐列表初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/packageList")
	public ModelAndView packageList()throws Exception{
		/*HttpSession session=request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		hospitalpackageCustom.setHospid(hospid);
		ModelAndView model = new ModelAndView();
		try {
			List<HospitalpackageCustom> packageList = packageService.getHospitalpackageList(hospitalpackageCustom);
			model.addObject("packageList", packageList);
		} catch (Exception e) {
			model.addObject("packageList", null);
		}*/
		ModelAndView model = new ModelAndView();
		model.setViewName("MainFrame/PackageMan/PackageList");
		return model;
	}
	/**
	 * 套餐列表的ajax加载
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/packageListAjax")
	public @ResponseBody Map<String,Object> packageListAjax(Integer limit,Integer offset,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		String param = "limit="+limit+"&offset="+offset+"&hosploginid="+hosploginid;//参数串
		
		String jsonobjectmsg = "";
		String jsonobjectcode ="";
		Integer total=0;
		List<Map<String,Object>> packagelist=new ArrayList<>();
		if(hosploginid!=null) {
			try {
				JSONObject Packagejson = HttpRequest.sendPost("getpackagelist", param);
				jsonobjectmsg = Packagejson.get("msg").toString();
				jsonobjectcode = Packagejson.get("code").toString();
				try {
					if(Packagejson.get("data").toString()!="null") {
						JSONObject jsonobjectdata = Packagejson.getJSONObject("data");
						JSONArray jsonarrayrows = jsonobjectdata.getJSONArray("rows");
						total=Integer.parseInt(jsonobjectdata.get("total").toString());
						packagelist = DataAnalyze.PackageJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
				}
			} catch (Exception e) {
				System.out.println("出错信息是："+e.getMessage());
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();  
		map.put("msg", jsonobjectmsg);
		map.put("rows", packagelist);
		map.put("code", jsonobjectcode);
        map.put("total", total);  
        
        System.out.println("输出这个map是："+map);
		return map;
	}
	/**
	 * 根据hosploginid和packageid修改这条记录
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/packageEditSubmit")
	public @ResponseBody Map<String,Object> packageEditSubmit(HttpServletRequest request)throws Exception {
		HttpSession session = request.getSession();
		Integer hosploginid=(Integer)session.getAttribute("hosploginid");
		
		String packageid = request.getParameter("packageid");
		String packagename = request.getParameter("packagename");
		String packagenumber = request.getParameter("packagenumber");
		String packageabs = request.getParameter("packageabs");
		String packagesustime = request.getParameter("packagesustime");
		String packageoperatetime = request.getParameter("packageoperatetime");
		String param = "packageid="+packageid+"&packagename="+packagename+"&packagenumber="+packagenumber+"&packageabs="+packageabs+"&packagesustime="+packagesustime+"&packageoperatetime="+packageoperatetime+"&hosploginid="+hosploginid;
		System.out.println("输出这个参数串param："+param);
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			JSONObject json = HttpRequest.sendPost("editpackage", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，输出错误信息："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输出错了，套餐修改失败！");
		}
		return result;
	}
	/**
	 * 根据packageid删除某个套餐
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/DeletePackage")
	public @ResponseBody Map<String,Object> DeletePackage(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer hosploginid = (Integer)session.getAttribute("hosploginid");
		Integer packageid = Integer.parseInt(request.getParameter("packageid"));
		
		String param = "hosploginid="+hosploginid+"&packageid="+packageid;
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			JSONObject json = HttpRequest.sendPost("deletepackage", param);
			String jsoncode = json.get("code").toString();
			String jsonmsg = json.get("msg").toString();
			
			result.put("code", jsoncode);
			result.put("msg",jsonmsg);
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据传输错误，套餐删除失败！");
		}
		return result;
	}
}
